using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Possan.Graphics.Filters
{
	public class OverlayFilter : IBaseFilter
	{
		public string ImageFile;
		public string Align;

		public string ParameterDefinition
		{
			get
			{
				     return null;
			}
		}

		public OverlayFilter()
		{
			Align = "top, left";
			ImageFile = "";
		}

		public Bitmap Filter(Bitmap bitmap)
		{
			Bitmap bmp2 = bitmap.Clone() as Bitmap;

		//	Logger.Info(this.GetType(), "Run");
			string[] align = Align.Split(',');

			int pctX = 0;
			int pctY = 0;

			if (align.Length >= 1)
			{
				align[0] = align[0].Trim().ToLower();
				Logger.Debug(this.GetType(), "horizontal align = " + align[0]);
				if (align[0] == "left")
					pctX = 0;
				if (align[0] == "center" ||
					align[0] == "middle")
					pctX = 50;
				if (align[0] == "right")
					pctX = 100;
			}

			if (align.Length >= 2)
			{
				align[1] = align[1].Trim().ToLower();
				Logger.Debug(this.GetType(), "vertical align = " + align[1]);
				if (align[1] == "top")
					pctY = 0;
				if (align[1] == "center" ||
					align[1] == "middle")
					pctY = 50;
				if (align[1] == "bottom")
					pctY = 100;
			}

			Logger.Debug(this.GetType(), "offset % = " + pctX + ", " + pctY);
			if (ImageFile != "")
			{
				Logger.Debug(this.GetType(), "imgname=" + ImageFile);
				Bitmap img = (Bitmap)Bitmap.FromFile(ImageFile);
				if (img != null)
				{
					img.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);

					Logger.Debug(this.GetType(), "img=" + img + " (" + img.Width + " x " + img.Height + ")");

					Logger.Debug(this.GetType(), "subject = (" + bitmap.Width + " x " + bitmap.Height + ")");

					System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp2);

					int offsetMaxX = bitmap.Width - img.Width;
					int offsetMaxY = bitmap.Height - img.Height;

					Logger.Debug(this.GetType(), "max offset = " + offsetMaxX + ", " + offsetMaxY);

					int x = (int)((offsetMaxX * pctX) / 100);
					int y = (int)((offsetMaxY * pctY) / 100);

					Logger.Debug(this.GetType(), "x=" + x + ", y=" + y);

					g.DrawImage(img, x, y);
				}
			}

			     return bmp2;
		}

	}

}





